#!/usr/bin/env python
#-*- coding:utf-8 -*-
from wwpy.io.Source import Source
from wwpy.util.Wrapper import looper

class BidTop(object):
    
    def __init__(self,date):
        self.date=date
        self._input_file='/work2/opt/analytic/precise/ads/user_auction/user_auction_%s' % (date)
        self._output_file='/work2/opt/analytic/precise/ads/tmp/bid_top/bid_top_%s' % (date)
        self._cates=dict((cate,0) for cate in ('保安','仓储/物流','普工','普工/技工/生产','司机','装卸/搬运工'))
        
    def bid_top(self,n=5):
        info={}
        s=Source(self._input_file)
        for line in s.read():
            fields=line.strip().split(',')
            city,cate,bid=fields[8],fields[5],float(fields[15])/100
            if not cate in self._cates:
                continue
            key=(city,cate)
            info.setdefault(key,[]).append(bid)
        for key in info:
            bids=info[key]
	    bids=list(set(bids))
	    bids.sort(reverse=True)
            if len(bids)>n:
                bids=bids[:n]
            for bid in bids:
                yield [self.date]+list(key)+[bid]
                
    def store(self):
        from wwpy.io.Writer import writer
        writer.sep(',')
        writer.output(self._output_file)
        writer.info(self.bid_top())
        writer.write()
        

@looper('DAY')
def main(date):
    bt=BidTop(date)
    bt.store()
    
if __name__ == '__main__':
    main()
    
